package Controller1;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.swing.JOptionPane;
import Dao.staffDao;
import Model1.staff;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.control.ToggleGroup;
import javafx.stage.FileChooser;
import javafx.stage.Stage;
import javafx.scene.control.RadioButton;
import java.lang.Integer;
public class RegisterController {
	@FXML
	private TextField name;
	@FXML
	private TextField id;
	@FXML
	private TextField phonenumber;
	@FXML
	private TextField password;
	@FXML
	private RadioButton servant;
	@FXML
	private RadioButton manager;
	
	@FXML
	private FileChooser fileChooser = new FileChooser();
	@FXML
	private Button button;
	@FXML
	public ToggleGroup tg;
	@FXML
	private Button Upload;
	
	List<Map<String,Object>> list = new ArrayList<>();
	private String path = null;

	
	public void initialize(){
		tg = new ToggleGroup();
		servant.setToggleGroup(tg);
		manager.setToggleGroup(tg);
		servant.setSelected(true); 
		
	}
	
	public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("View1/Register.fxml"));
        primaryStage.setScene(new Scene(root));
        primaryStage.setResizable(false);
        primaryStage.show();
    }
	
	public void returnMain() {
		try {
		Stage stage = (Stage)name.getScene().getWindow();
		stage.close();
		Logincontroller lc = new Logincontroller();
		lc.start(stage);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public void register() {
		try {
			String a="";
			if(servant.isSelected())
				a="服务员";
			else if(manager.isSelected())
				a="管理员";
			staff stf=new staff(name.getText(),Integer.parseInt(id.getText().trim()),a,phonenumber.getText(),password.getText());
			stf.setImage(getFileInputStream());
			Boolean b=staffDao.Insert(stf);
			if(b)
			{
				JOptionPane.showMessageDialog(null, "注册成功"); 
				
			}
		}catch(Exception e) {
			JOptionPane.showMessageDialog(null, "注册失败，请重试"); 
			e.printStackTrace();
		}
	}
	
	public void Upload() {
		Stage stage = null;
		File file = fileChooser.showOpenDialog(stage);
		path = file.getPath();
		
		
	}
	
	public FileInputStream getFileInputStream() {
		if(path!=null) {
			File file = new File(path);
	        try {
	        	FileInputStream FISimage = new FileInputStream(file);
	        	return FISimage;
	        } catch (FileNotFoundException e) {
	            e.printStackTrace();
	        }
		}
		return null;
	}
}
